package room_event

import (
	"encoding/json"
	"longmen/server/internal/rpc/tencentim/chat"
	"longmen/server/internal/rpc/tencentim/messages"
	"longmen/server/internal/rpc/tencentim/utils"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
	pb "longmen/server/pkg/protobuf"
	"time"
)

/*
返回修改后的礼物消息
*/
func handlingImmeGiftMessage(user *chat.ClientUser, b []byte) (ret *pb.CallbackResp, err error) {
	fName := "ImmeGift"
	//去user_info数据库里写下相应的值
	oMsg := &messages.ImmeGiftMsg{}
	err = json.Unmarshal(b, oMsg)
	if err != nil {
		return utils.CallbackFailResp(nil, err.Error()), nil
	}
	oUserInfo := &models_main.UserInfo{}
	err = mysql.GetXAnyParam(oUserInfo, map[string]interface{}{"uid": user.UserId}, true, fName)
	if err != nil {
		return utils.CallbackFailResp(nil, err.Error()), nil
	}
	if oUserInfo.Id == 0 {
		oUserInfo.Uid = uint(user.UserId)
		oUserInfo.ImmeGift = uint(oMsg.Value)
		oUserInfo.CreatedTime = time.Now().Unix()
		oUserInfo.UpdatedTime = 0
		_, err = mysql.InsertXAny(oUserInfo, fName)
	} else {
		if oUserInfo.ImmeGift != uint(oMsg.Value) {
			oUserInfo.UpdatedTime = time.Now().Unix()
			err = mysql.UpdateXAnyParam(nil, []string{"imme_gift"}, oUserInfo, fName)
		}
	}
	if err != nil {
		return utils.CallbackFailResp(nil, err.Error()), nil
	}
	ret = &pb.CallbackResp{}
	ret.ErrorCode = 0

	return utils.CallbackOkResp(nil), nil
}
